
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using AnovoAndes.Framework.Utilidades;
using NovaSystem.EntidadesNegocio;

namespace NovaSystem.AccesoDatos
{
	public class loginDAO 
	{
			
		#region [Variables Publicas]
        List<string> listaEsquema = new List<string>();
		System.Configuration.AppSettingsReader read = new System.Configuration.AppSettingsReader();
        #endregion
		
		#region Constructores
		public loginDAO()
		{
		}
		#endregion
		
		#region Metodos Privados
		
		 private List<string> LlenarEsquema(IDataReader dr)
        {
            DataTable schemaTable;
            schemaTable = dr.GetSchemaTable();
            listaEsquema = new List<string>();
            const int columnaNombre = 0;
            //Para cada campo de la tabla...
            foreach (DataRow myField in schemaTable.Rows)
            {
                //Agrega nombre del campo a la lista generica.
                listaEsquema.Add(myField[columnaNombre].ToString());
            }
            return listaEsquema;

        }
		
		private login Crear(IDataReader dr)
		{
			login vObj = new login();
			try
			{
				listaEsquema.Sort();
							if (listaEsquema.BinarySearch("usuario",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["usuario"] is System.DBNull))
					{
						vObj.usuario= (string)dr["usuario"];
					}
				}
				if (listaEsquema.BinarySearch("fechaLogin",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["fechaLogin"] is System.DBNull))
					{
						vObj.fechaLogin= (DateTime)dr["fechaLogin"];
					}
				}
				if (listaEsquema.BinarySearch("sucursal",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["sucursal"] is System.DBNull))
					{
						vObj.sucursal= (string)dr["sucursal"];
					}
				}

				return vObj;
			}
			catch(Exception ex)
			{
				throw new Exception(ex.Message);
			}
		}
		
		private List<login> CrearDTOs(IDataReader dr) 
		{
			List<login> listaDTO = new List<login>();
			if 	(dr != null)
			{
				listaEsquema = LlenarEsquema(dr);// Validadores.LlenarEsquema(dr);
				
				while (dr.Read())
				{
					listaDTO.Add(Crear(dr));
				}
				dr.Close();
			}
			return listaDTO;
		}
		
		#endregion
		
		#region Metodos Publicos
        public login BuscaLogin(string usuario)
        {
            MySqlConnection conn = new MySqlConnection();
            List<login> lista = new List<login>();
            try
            {
                string connString = read.GetValue("Database", typeof(string)).ToString();
                conn.ConnectionString = connString;
                conn.Open();
                MySqlCommand objDbCommand = new MySqlCommand();
                objDbCommand.Connection = conn;
                objDbCommand.CommandType = CommandType.StoredProcedure;
                objDbCommand.CommandText = "usp_S_buscaLogin";
                objDbCommand.Parameters.AddWithValue("_usuario", usuario);
                using (IDataReader objDataReader = objDbCommand.ExecuteReader())
                {
                    lista = CrearDTOs(objDataReader);
                }
                CerrarConn(conn);
                if (lista.Count > 0)
                    return lista[0];
                else
                    return new login();
            }
            catch (Exception ex)
            {
                CerrarConn(conn);
                throw new Exception(ex.Message);
            }
        }

		public List<login> Listar()
		{
			MySqlConnection conn = new MySqlConnection();
			List<login> lista = new List<login>();
			try
			{
				string connString = read.GetValue("Database",typeof(string)).ToString();
				conn.ConnectionString = connString;
				conn.Open();
				MySqlCommand objDbCommand = new MySqlCommand();
				objDbCommand.Connection = conn;
				objDbCommand.CommandType = CommandType.StoredProcedure;
				objDbCommand.CommandText = "usp_s_login";
				
				using (IDataReader objDataReader = objDbCommand.ExecuteReader())
				{
					lista = CrearDTOs(objDataReader);
				}
				CerrarConn(conn);
				return lista;
			}
			catch(Exception ex)
			{
				CerrarConn(conn);
				throw new Exception(ex.Message);
			}
		}
		
		
		private void CerrarConn(MySqlConnection conn)
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
                conn.Dispose();
            }
        }
		
		public string Insertar( login dto)
		{
			MySqlConnection conn = new MySqlConnection();
			try
			{
				string connString = read.GetValue("Database",typeof(string)).ToString();
				conn.ConnectionString = connString;
				conn.Open();
				MySqlCommand objDbCommand = new MySqlCommand();
				objDbCommand.Connection = conn;
				objDbCommand.CommandType = CommandType.StoredProcedure;
				objDbCommand.CommandText = "usp_I_login";
				objDbCommand.Parameters.AddWithValue("_usuario",AssertNull.getValue(dto.usuario));
				objDbCommand.Parameters.AddWithValue("_sucursal",AssertNull.getValue(dto.sucursal));
				objDbCommand.ExecuteNonQuery();
				CerrarConn(conn);
				return "";
			}
			catch(Exception ex)
			{
				CerrarConn(conn);
				throw new Exception(ex.Message);
			}
		}
		
		public void Eliminar(login dto)
		{
			MySqlConnection conn = new MySqlConnection();
			try
			{
				string connString = read.GetValue("Database",typeof(string)).ToString();
				conn.ConnectionString = connString;
				conn.Open();
				MySqlCommand objDbCommand = new MySqlCommand();
				objDbCommand.Connection = conn;
				objDbCommand.CommandType = CommandType.StoredProcedure;
				objDbCommand.CommandText = "usp_D_login";
								objDbCommand.Parameters.AddWithValue("_usuario",AssertNull.getValue(dto.usuario));

				objDbCommand.ExecuteNonQuery();
				CerrarConn(conn);
			}
			catch(Exception ex)
			{
				CerrarConn(conn);
				throw new Exception(ex.Message);
			}
		}
		
		#endregion
		
	}
}
